#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/2/19 13:04
@desc
"""

import select
import socket


sk = socket.socket()
sk.bind((socket.gethostname(), 8000))
sk.setblocking(False)
sk.listen(1)

read_lst = [sk]

while True:
    # select.select()方法，只监听客户端发起的sk.connect()和sk.send()方法，所以
    # 当接收到sk.connect()调用时，返回的r_lst里面就只有sk对象，
    # 当接收到sk.send()调用时，返回的r_lst里面就只有conn对象，
    r_lst, w_lst, x_lst = select.select(read_lst, [], [])
    print(r_lst)
    for i in r_lst:
        if i is sk:
            conn, addr = i.accept()
            read_lst.append(conn)
        else:
            ret = i.recv(1024)
            if ret == b'':
                i.close()
                read_lst.remove(i)
                continue
            print(ret)
            i.send(b'goodbye')




